package com.hzy.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hzy
 * @since 2022/2/28 21:08
 */
@RequestMapping("/flow")
@RestController
public class FlowLimitControl {

    @RequestMapping("/getA")
    public String getA(){
        return "------- getA";
    }


    @RequestMapping("/getB")
    public String getB(){
        return "------- getB";
    }

    /**
     * hotKey 测试
     * 热点参数限流
     * */
    @SentinelResource(value = "hot_key", blockHandler = "block_hot_key")
    @GetMapping("/hotKey")
    public String hotKey(@RequestParam(value = "p1", required = false) String p1,
                         @RequestParam(value = "p2", required = false) String p2)
    {
        return "hot key test ok," + p1 + " " + p2;
    }
    public String block_hot_key(String p1, String p2, BlockException e){
        return "block_hot_key," + p1 + " " + p2;
    }


    /**
     * SentinelResource blockHandlerClass 测试
     * */
    @SentinelResource(
            value = "blockHandlerClass",
            blockHandlerClass = {CustomerBlockHandler.class},
            blockHandler = "handlerV1")
    @GetMapping("/blockHandlerClass")
    public String blockHandlerClass(@RequestParam(value = "p1", required = false) String p1)
    {
        return "customBlockHandler test ok," + p1;
    }


}
